import requests
import re
import os

ret = requests.get("https://www.sucai8.com/")
# print(ret.text)
head = "https://www.sucai8.com"
# rets_1 = re.findall(r'(?<=<img src=")(.+)(?=" class.+/>)', ret.text)
rets_2 = re.findall(r'(?<=<img src=")(.+?)(?=" (?:alt=.+>|width.+>|class.+/>))', ret.text)
# print(rets_1)
# print(rets_2)
# print(len(rets_2))
# print(len(rets))
# for temp in rets_2:
#     print(head + temp)
list_result = [head+temp for temp in rets_2]

for i in range(len(list_result)):
    print(f"正在下载第{i+1}张图片，下载地址：{list_result[i]}")
    try:
        d_pic = requests.get(list_result[i], timeout=5)

    except TimeoutError:
        print("访问超时，该图片无法下载")

    else:
        d_path = os.path.dirname(__file__)
        de_path = d_path + f'/{i+1}' + f'.{list_result[i].split(".")[3]}'
        with open(de_path, 'wb') as fp:
            fp.write(d_pic.content)
            fp.close()


